import _ from 'lodash-es'
import { objectMergLeft } from '@/utils/common'
import { useUserStore } from '@/store'
/**
 * apis single\multiple\reject\info
 */
export const useAuditPro = ({
  // name = '',
  initForm = {},
  apis = {},
  handleChange,
  onShow,
  onReady,
  formatParams,
  handleReset,
}) => {
  //表单数据
  const model = ref(_.cloneDeep(initForm))
  //表单ref
  const $form = ref(null)
  //数据表格ref
  const $table = ref(null)
  // 表单操作类型 single|multiple|reject|view
  const type = ref('')
  async function showForm(actionType, data = {}, callBack = () => {}) {
    type.value = actionType
    $form.value.setState('title', actionType == 'view' ? `查看` : `审核处理`)
    $form.value.setState('showFlag', true)
    if (typeof onShow === 'function') {
      onShow(type.value)
    }
    if (typeof callBack === 'function') {
      callBack()
    }
    model.value = _.cloneDeep(initForm)
    if (type.value !== 'multiple') {
      if (typeof apis.info === 'function') {
        // 异步初始化
        $form.value.setState('loadingFlag', true)
        const result = await apis.info(data)
        model.value = objectMergLeft(model.value, result)
        if (type.value == 'single') {
          model.value.reviewStatus = 1
        }
        if (type.value == 'reject') {
          model.value.reviewStatus = 2
        }
        $form.value.setState('loadingFlag', false)
      } else {
        // 同步初始化
        model.value = objectMergLeft(model.value, data)
        if (type.value == 'single') {
          model.value.reviewStatus = 1
        }
        if (type.value == 'reject') {
          model.value.reviewStatus = 2
        }
      }
    } else {
      // 同步初始化
      model.value = objectMergLeft(model.value, data)
      model.value.reviewStatus = 1
    }
    if (typeof onReady === 'function') {
      onReady(type.value)
    }
  }
  async function handleSubmit(params) {
    if (!['multiple', 'single', 'reject'].includes(type.value)) return
    let p = params
    if (typeof formatParams === 'function') {
      p = await formatParams(_.cloneDeep(params))
    }
    $form.value.setState('submitFlag', true)
    apis[type.value](p)
      .then((res) => {
        $form.value.setState('submitFlag', false)
        $form.value.setState('showFlag', false)
        onChange(type.value, res)
      })
      .catch((err) => {
        console.log('err:', err)
        $form.value.setState('submitFlag', false)
      })
  }
  // 多个审核
  function handleMultipleAudit(data, callBack) {
    showForm('multiple', data, callBack)
  }
  // 单个审核
  function handleSingleAudit(data, callBack) {
    showForm('single', data, callBack)
  }
  // 拒绝
  function handleReject(data, callBack) {
    showForm('reject', data, callBack)
    // const dialog = $dialog.warning({
    //   title: '提示',
    //   content: '确定拒绝？',
    //   negativeText: '取消',
    //   positiveText: '确定',
    //   positiveButtonProps: {
    //     color: '#0A56D8FF',
    //   },
    //   async onPositiveClick() {
    //     try {
    //       dialog.loading = true
    //       const res = await apis.reject(data)
    //       dialog.loading = false
    //       onChange('reject', res)
    //     } catch (error) {
    //       console.log('error:', error)
    //       dialog.loading = false
    //     }
    //   },
    //   ...options,
    // })
  }
  // 查看
  function handleView(data, callBack) {
    showForm('view', data, callBack)
  }
  function onChange(actionType, res) {
    if (typeof handleChange === 'function') {
      handleChange(actionType, res, model)
    } else {
      $message.success(`审核处理成功!`)
      const userStore = useUserStore()
      userStore.getUnauditedCount()
      $table?.value?.handleQuery()
      if (typeof handleReset === 'function') {
        handleReset(type.value)
      }
    }
  }
  return {
    type,
    model,
    $form,
    $table,
    handleMultipleAudit,
    handleSingleAudit,
    handleReject,
    handleSubmit,
    handleView,
  }
}
